package com.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.entity.AccountEntity;
import com.entity.CourseEntity;
import com.entity.DepartmentEntity;
import com.entity.FileEntity;
import com.entity.LoginEntity;
import com.entity.PyqEntity;
import com.entity.ScoreEntity;
import com.entity.StudentEntity;
import com.entity.TeacherEntity;
import com.entity.majorEntity;
import com.opensymphony.xwork2.ActionSupport;
import com.service.spaceService;

public class spaceAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private LoginEntity loginEntity;
	private spaceService l_spaceService = new spaceService();
	
	private StudentEntity studentEntity = new StudentEntity();
	private List<ScoreEntity> studentScoreEntities = new ArrayList<ScoreEntity>();
	private List<CourseEntity> studentCourseEntities = new ArrayList<CourseEntity>();
	private CourseEntity courseEntity = new CourseEntity();
	private List<PyqEntity> pyqEntities = new ArrayList<PyqEntity>();
	
	private TeacherEntity teacherEntity = new TeacherEntity();
	private List<CourseEntity> teacherCourseEntities = new ArrayList<CourseEntity>();
	
	private List<FileEntity> storedFileEntities = new ArrayList<FileEntity>();
	private List<DepartmentEntity> depatmentEntities = new ArrayList<DepartmentEntity>();
	private List<majorEntity> majorEntities = new ArrayList<majorEntity>();
	private List<StudentEntity> studentEntities = new ArrayList<StudentEntity>();
	private List<TeacherEntity> teacherEntities = new ArrayList<TeacherEntity>();
	private List<CourseEntity> courseEntities = new ArrayList<CourseEntity>();
    private List<File> files; 
    private FileEntity uploadFileEntity = new FileEntity();
    private FileEntity fileEntity = new FileEntity();
    private String fileTypeString = new String();
    private String fileNameString = new String();
    private majorEntity majorentity = new majorEntity();
    private DepartmentEntity departmentEntity = new DepartmentEntity();
	
	public LoginEntity getLoginEntity() {
		return loginEntity;
	}

	public void setLoginEntity(LoginEntity loginEntity) {
		this.loginEntity = loginEntity;
	}

	public StudentEntity getStudentEntity() {
		return studentEntity;
	}

	public void setStudentEntity(StudentEntity studentEntity) {
		this.studentEntity = studentEntity;
	}

	public List<ScoreEntity> getStudentScoreEntities() {
		return studentScoreEntities;
	}

	public void setStudentScoreEntities(List<ScoreEntity> studentScoreEntities) {
		this.studentScoreEntities = studentScoreEntities;
	}

	public List<CourseEntity> getStudentCourseEntities() {
		return studentCourseEntities;
	}

	public void setStudentCourseEntities(List<CourseEntity> studentCourseEntities) {
		this.studentCourseEntities = studentCourseEntities;
	}

	public TeacherEntity getTeacherEntity() {
		return teacherEntity;
	}

	public void setTeacherEntity(TeacherEntity teacherEntity) {
		this.teacherEntity = teacherEntity;
	}

	public List<CourseEntity> getTeacherCourseEntities() {
		return teacherCourseEntities;
	}

	public void setTeacherCourseEntities(List<CourseEntity> teacherCourseEntities) {
		this.teacherCourseEntities = teacherCourseEntities;
	}

	public List<FileEntity> getStoredFileEntities() {
		return storedFileEntities;
	}

	public void setStoredFileEntities(List<FileEntity> storedFileEntities) {
		this.storedFileEntities = storedFileEntities;
	}

	public List<DepartmentEntity> getDepatmentEntities() {
		return depatmentEntities;
	}

	public void setDepatmentEntities(List<DepartmentEntity> depatmentEntities) {
		this.depatmentEntities = depatmentEntities;
	}

	public List<majorEntity> getMajorEntities() {
		return majorEntities;
	}

	public void setMajorEntities(List<majorEntity> majorEntities) {
		this.majorEntities = majorEntities;
	}

	public List<StudentEntity> getStudentEntities() {
		return studentEntities;
	}

	public void setStudentEntities(List<StudentEntity> studentEntities) {
		this.studentEntities = studentEntities;
	}

	public List<TeacherEntity> getTeacherEntities() {
		return teacherEntities;
	}

	public void setTeacherEntities(List<TeacherEntity> teacherEntities) {
		this.teacherEntities = teacherEntities;
	}

	public List<CourseEntity> getCourseEntities() {
		return courseEntities;
	}

	public void setCourseEntities(List<CourseEntity> courseEntities) {
		this.courseEntities = courseEntities;
	}

	public String getFileTypeString() {
		return fileTypeString;
	}

	public void setFileTypeString(String fileTypeString) {
		this.fileTypeString = fileTypeString;
	}

	public List<File> getFiles() {
		return files;
	}

	public void setFiles(List<File> files) {
		this.files = files;
	}

	public FileEntity getUploadFileEntity() {
		return uploadFileEntity;
	}

	public void setUploadFileEntity(FileEntity uploadFileEntity) {
		this.uploadFileEntity = uploadFileEntity;
	}

	public String getFileNameString() {
		return fileNameString;
	}

	public void setFileNameString(String fileNameString) {
		this.fileNameString = fileNameString;
	}

	public FileEntity getFileEntity() {
		return fileEntity;
	}

	public void setFileEntity(FileEntity fileEntity) {
		this.fileEntity = fileEntity;
	}

	public CourseEntity getCourseEntity() {
		return courseEntity;
	}

	public void setCourseEntity(CourseEntity courseEntity) {
		this.courseEntity = courseEntity;
	}

	public majorEntity getMajorentity() {
		return majorentity;
	}

	public void setMajorentity(majorEntity majorentity) {
		this.majorentity = majorentity;
	}

	public DepartmentEntity getDepartmentEntity() {
		return departmentEntity;
	}

	public void setDepartmentEntity(DepartmentEntity departmentEntity) {
		this.departmentEntity = departmentEntity;
	}

	public String showSpace() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		if(loginEntity.getType().equals("student")) {
			studentEntity = l_spaceService.getStudentEntity(loginEntity);
			loginEntity.setCurrentTask(1);
		}else if (loginEntity.getType().equals("teacher")) {
			teacherEntity = l_spaceService.getTeacherEntity(loginEntity);
			teacherCourseEntities = l_spaceService.getCourseByTeacherEntities(teacherEntity);
			loginEntity.setCurrentTask(1);
		}else if (loginEntity.getType().equals("admin")) {
			loginEntity.setCurrentTask(1);
		}else if (loginEntity.getType().equals("visitor")) {
			
		}
		return "showspace";
	}
	
	public String getStudentInfo() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		studentEntity = l_spaceService.getStudentEntity(loginEntity);
		loginEntity.setCurrentTask(1);
		return "showspace";
	}	
	public String getStudentScore() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		studentScoreEntities = l_spaceService.getStudentScoreEntities(loginEntity);
		loginEntity.setCurrentTask(2);
		return "showspace";
	}
	
	public String getSchedule() {
		System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		studentScoreEntities = l_spaceService.getStudentScoreEntities(loginEntity);
		studentCourseEntities.clear();
		for(int i=0;i<studentScoreEntities.size();i++) {
			studentCourseEntities.add(l_spaceService.getCourseByScoreEntities(studentScoreEntities.get(i)));
		}
		loginEntity.setCurrentTask(3);
		return "showspace";
	}
	
	public String gotoCourseSelectSys() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		studentEntity = l_spaceService.getStudentEntity(loginEntity);
		studentCourseEntities.clear();
		studentCourseEntities = l_spaceService.getSelectableCourseByStudentEntities(studentEntity);
		loginEntity.setCurrentTask(4);
		return "showspace";
	}
	
	public String studentChoiceCourse() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		// 通过courseEntity 找出完整的courseRntity
		// 将courseEntity studentEntity 组合成 scoreEntity填入scoreTable
		ScoreEntity scoreEntity = new ScoreEntity();
		System.out.println("select course");
		courseEntity = l_spaceService.getCourseEntityByCourseNoAndTeacherNo(courseEntity.getCourseNo(),courseEntity.getTeacherNo());
		System.out.println("select course1");
		scoreEntity.setCourseName(courseEntity.getCourseName());
		System.out.println("select course2");
		scoreEntity.setCourseNo(courseEntity.getCourseNo());
		System.out.println("select course3");
		scoreEntity.setStudentNo(studentEntity.getStudentNo());
		System.out.println("select course4");
		scoreEntity.setTeacherNo(courseEntity.getTeacherNo());
		System.out.println("select course5");
		scoreEntity.setScore((float)0.0);
		System.out.println("select course6");
		scoreEntity.setIndex("1");
		l_spaceService.addScoreToTable(scoreEntity);
		System.out.println("select course7");
		return "showspace";
	}
	
	
	// TODO:
	public String gotoFriendsCircle() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		pyqEntities = l_spaceService.getPyqEntities();
		loginEntity.setCurrentTask(5);
		return "showspace";
	}
	
	public String getTeacherInfo() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		teacherEntity = l_spaceService.getTeacherEntity(loginEntity);
		teacherCourseEntities = l_spaceService.getCourseByTeacherEntities(teacherEntity);
		loginEntity.setCurrentTask(1);
		return "showspace";
	}
	
	public String gotoCourseSetting() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		teacherEntity = l_spaceService.getTeacherEntity(loginEntity);
		teacherCourseEntities = l_spaceService.getCourseByTeacherEntities(teacherEntity);
		loginEntity.setCurrentTask(2);
		return "showspace";
	}
	
	public String teacherSettingCourse() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		CourseEntity lCourseEntity = new CourseEntity();
		teacherEntity = l_spaceService.getTeacherEntity(loginEntity);
		String selectable = courseEntity.getSelect_enable();
		// 通过coureseNo 和 teacherNo 找到courseTable对应的项赋值给course
		System.out.println(courseEntity.getSelect_enable() + courseEntity.getCourseNo());
		lCourseEntity = l_spaceService.getCourseEntityByCourseNoAndTeacherNo(courseEntity.getCourseNo(), teacherEntity.getTeacherNo());
		System.out.println("this");
		lCourseEntity.setSelect_enable(selectable);
		lCourseEntity.setIndex("1");
		l_spaceService.updateCourse(lCourseEntity);
		// 修改course的select_able为对应的值
		// 更新course在表中的值
		return "showspace";
	}
	
	public String announceManage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		storedFileEntities = l_spaceService.getFileEntities();
		loginEntity.setCurrentTask(1);
		return "showspace";
	}
	public String addAnnounce() {
		return "showupload";
	}
	public String uploadFile() throws FileNotFoundException, IOException {
		uploadFileEntity.setFileName(fileNameString);
		uploadFileEntity.setFileType(fileTypeString);
		l_spaceService.uploadFile(uploadFileEntity,uploadFileEntity.getFileType(),files);
		return "showspace";
	}
	public String deleteAnnounce() throws IOException {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		//通过提供的filename找到完整的fileEntity
		System.out.println("You want to delete" + fileEntity.getFileName());
		fileEntity = l_spaceService.getFileEntityByfileName(fileEntity.getFileName());
		l_spaceService.deleteFile(fileEntity);
		l_spaceService.deleteFileEntity(fileEntity);
		return "showspace";
	}
	public String depmajManage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		depatmentEntities = l_spaceService.getDepartmentEntities();
		for(int i=0;i<depatmentEntities.size();i++) {
			System.out.println(depatmentEntities.get(i).getDepartmentName());
		}
		majorEntities = l_spaceService.getmajorEntities();
		for(int i=0;i<majorEntities.size();i++) {
			System.out.println(majorEntities.get(i).getMajorName());
		}
		loginEntity.setCurrentTask(2);
		return "showspace";
	}
	public String addMajor() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		l_spaceService.addMajorEntity(majorentity);
		return "showspace";
	}
	public String updateMajor() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		return "showupdatemajor";
	}
	public String deleteMajor() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		majorentity = l_spaceService.getMajorEntityByMajorNo(majorentity.getMajorNo());
		l_spaceService.deleteMajorEntity(majorentity);
		return "showspace";
	}
	public String gotoMajorAddPage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		depatmentEntities = l_spaceService.getDepartmentEntities();
		return "showaddmajor";
	}
	public String addDepartment() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		l_spaceService.addDepartmentEntity(departmentEntity);
		return "showspace";
	}
	public String gotoDepartmentAddPage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		//depatmentEntities = l_spaceService.getDepartmentEntities();
		return "showadddeparment";
	}
	
	public String studentManage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		studentEntities = l_spaceService.getStudentEntities();
		loginEntity.setCurrentTask(3);
		return "showspace";
	}
	public String deleteStudent() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		//storedFileEntities = l_spaceService.getFileEntities();
		//loginEntity.setCurrentTask(1);
		studentEntity = l_spaceService.getStudentEntityByStudentNo(studentEntity.getStudentNo());
		l_spaceService.deleteStudentEntity(studentEntity);
		return "showspace";
	}
	public String addStudent() {
		l_spaceService.addStudentEntity(studentEntity);
		AccountEntity accountEntity = new AccountEntity();
		accountEntity.setAccount(studentEntity.getStudentNo());
		accountEntity.setPassword(studentEntity.getStudentNo());
		accountEntity.setType("student");
		l_spaceService.addAccountEntity(accountEntity);
		return "showspace";
	}
	public String gotoStudentAddPage() {
		depatmentEntities = l_spaceService.getDepartmentEntities();
		majorEntities = l_spaceService.getmajorEntities();
		return "showaddstudent";
	}
	public String teacherManage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		teacherEntities = l_spaceService.getTeacherEntities();
		loginEntity.setCurrentTask(4);
		return "showspace";
	}
	public String deleteTeacher() {
		teacherEntity = l_spaceService.getTeacherEntityByTeacherNo(teacherEntity.getTeacherNo());
		l_spaceService.deleteTeacherEntity(teacherEntity);
		return "showspace";
	}
	public String addTeacher() {
		l_spaceService.addTeacherEntity(teacherEntity);
		AccountEntity accountEntity = new AccountEntity();
		accountEntity.setAccount(teacherEntity.getTeacherNo());
		accountEntity.setPassword(teacherEntity.getTeacherNo());
		accountEntity.setType("teacher");
		l_spaceService.addAccountEntity(accountEntity);
		return "showspace";
	}
	public String gotoTeacherAddPage() {
		depatmentEntities = l_spaceService.getDepartmentEntities();
		//majorEntities = l_spaceService.getmajorEntities();
		return "showaddteacher";
	}
	public String courseManage() {
		//System.out.println(loginEntity.getUseraccount() +" "+ loginEntity.getType());
		courseEntities = l_spaceService.getCourseEntities();
		loginEntity.setCurrentTask(5);
		return "showspace";
	}
	
	public String deleteCourse() {
		courseEntity = l_spaceService.getCourseEntityByCourseNo(courseEntity.getCourseNo());
		l_spaceService.deleteCourseEntity(courseEntity);
		return "showspace";
	}
	
	public String gotoCourseAddPage() {
		//depatmentEntities = l_spaceService.getDepartmentEntities();
		majorEntities = l_spaceService.getmajorEntities();
		teacherEntities = l_spaceService.getTeacherEntities();
		return "showaddcourse";
	}
	
	public String addCourse() {
		courseEntity.setIndex("1");
		courseEntity.setSelect_enable("true");
		l_spaceService.addCourseEntity(courseEntity);
		return "showspace";
	}
	





	
	
}
